////////////////////////////////////////////////////////////////////////////
//
// This is a sample which shows how to use the 32-bit door interface
// (DOOR32.DLL).
//
// Basically you just call DoorInitialize to start up, and DoorShutdown
// to clean up.  Writing to the port is done with DoorWrite, no translation
// of any kind is done, just raw output.  DoorRead will read characters
// from the input buffer.  It just returns with no characters read if there
// isn't anything to read (it does not block).
//
// The Wildcat version of DOOR32.DLL also hooks you up in the user's context
// in the Wildcat server, so you can call Wildcat server API functions.  The
// example here gets the user name.
//
////////////////////////////////////////////////////////////////////////////


#include <windows.h>
#include <stdio.h>
#pragma hdrstop

#include "door32.h"
#include "wcserver.h"

#pragma comment(lib,"door32.lib")
#pragma comment(lib,"wcsrv2.lib")

void Wprintf(const char *s, ...)
{
  va_list args;
  va_start(args, s);
  char buf[256];
  vsprintf(buf, s, args);
  va_end(args);
  DoorWrite((BYTE *)buf, strlen(buf));
}

int main(int, char *[])
{
  if (!DoorInitialize()) {
    printf("Could not initialize door\n\n");
    printf("This program must be run as a 32-bit door from Wildcat.\n");
    Sleep(5000);
    return 0;
  }
  TUser User;
  WildcatLoggedIn(&User);
  Wprintf("Welcome to Doortest, %s.\r\n\r\n", User.Info.Name);
  BOOL done = FALSE;
  Wprintf("Press a key or ESCAPE to quit: ");
  while (!done) {
    HANDLE a[2];
    a[0] = DoorGetAvailableEventHandle();
    a[1] = DoorGetOfflineEventHandle();
    switch (WaitForMultipleObjects(2, a, FALSE, INFINITE)) {
      case WAIT_OBJECT_0:
        BYTE c;
        DoorRead(&c, 1);
        if (c == 27) {
          done = TRUE;
        }
        //Wprintf("\r\nYou pressed %c (ascii %d)\r\n", c, c);
        Wprintf("%c", c);
        if (c==13) Wprintf("\r");
        break;
      case WAIT_OBJECT_0+1:
        done = TRUE;
        break;
    }
  }
  Wprintf("\r\n\r\nReturning to bbs...\r\n");
  DoorShutdown();
  return 0;
}
